﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOMS.DAL;
using BOMS.Utility;

namespace BOMS.WebUI.Web.RealStatus
{
    public partial class groupDetailInfo : System.Web.UI.Page
    {
        public string WinceIP
        {
            get { return Request.QueryString["winceIp"]; }
        }
        public string WinceMac
        {
            get { return Request.QueryString["winceMac"]; }
        }
        public string LanName
        {
            get { return Request.QueryString["lanName"]; }
        }
        public string WinceName
        {
            get { return Request.QueryString["winceName"]; }
        }
        public string GroupId
        {
            get { return Request.QueryString["groupId"]; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                
                                    //设置排序
                this.GridView_detail.Attributes.Add("SortExpression", "BatteryId");
                this.GridView_detail.Attributes.Add("SortDirection", "ASC");

                ViewState["SortOrder"] = "BatteryId";
                ViewState["OrderDire"] = " ASC";
                btn_back.PostBackUrl = "groupDetail.aspx?winceIp=" + WinceIP + "&WinceMac=" + WinceMac + "&winceName=" + HttpUtility.UrlEncodeUnicode(WinceName) + "";
                dbind();
            }
        }
        //protected void Page_Load(object sender, EventArgs e)
        //{

        //    if (!IsPostBack)
        //    {
        //        this.btn_back.PostBackUrl = "groupDetail.aspx?winceIp=" + Request.QueryString["winceIp"].ToString() + "&WinceMac=" + Request.QueryString["WinceMac"].ToString();
        //        dbind();
        //    }
        //}
        public void dbind()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView_detail.Attributes["SortExpression"];
            string sortDirection = this.GridView_detail.Attributes["SortDirection"];
            // 调用业务数据获取方法
            DataSet ds = RealDate.getBatteryInfoByGroupId(WinceMac, GroupId);
            DataTable dt = ds.Tables[0];

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }

            this.title.Text = "" + WinceName + "现场主机（IP:" + WinceIP + "） 电池组:" + GroupId + " 实时监控数据";
            this.GridView_detail.DataSource = dt;
            this.GridView_detail.DataBind();
        }

        protected void GridView_detail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //排序图标
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ShowSortedIcon(GridView_detail, e.Row);
            }
            if (e.Row.RowType == DataControlRowType.DataRow)//判断是不是数据行
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#abcdef'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                Literal ltrErrorDescr = e.Row.FindControl("ltrErrorDescr") as Literal;
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (ltrErrorDescr != null
                    && drv != null)
                {
                    ltrErrorDescr.Text = Common.BuildWarningString(false, (int)drv["StatusNO"],true);
                }
            }
        }
        protected void ShowSortedIcon(GridView gvData, GridViewRow gvRow)
        {
            for (int index = 0; index <= gvData.Columns.Count - 1; index++)
            {
                if ((gvData.Columns[index].SortExpression == gvSortExpression) && (gvData.Columns[index].SortExpression != ""))
                {
                    Image img = new Image();
                    if ((string)ViewState["OrderDire"] == " ASC")
                    {
                        img.ImageUrl = "../../Images/asc.png";
                        img.ToolTip = "递增排列";
                    }
                    else
                    {
                        img.ImageUrl = "../../Images/desc.png";
                        img.ToolTip = "递减排列";
                    }
                    gvRow.Cells[index].Controls.Add(img);
                }
            }
        }
        private string gvSortExpression
        {
            get
            {
                if (ViewState["SortOrder"] != null && ViewState["SortOrder"].ToString() != string.Empty)
                { return ViewState["SortOrder"].ToString(); }
                else
                { return ""; }
            }
            set
            { ViewState["SortOrder"] = value; }
        }   
        protected void GridView_detail_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();
            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView_detail.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView_detail.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");

                if (ViewState["OrderDire"].ToString() == " Desc")
                {
                    ViewState["OrderDire"] = " ASC";
                    ViewState["SortOrder"] = sortExpression;

                }
                else
                {
                    ViewState["OrderDire"] = " Desc";
                    ViewState["SortOrder"] = sortExpression;
                }
            }

            // 重新设定GridView排序数据列及排序方向
            this.GridView_detail.Attributes["SortExpression"] = sortExpression;
            this.GridView_detail.Attributes["SortDirection"] = sortDirection;
            gvSortExpression = e.SortExpression;
            dbind();
        }
    }
}